package gui.view;

import java.awt.Dimension;
import java.awt.FlowLayout;

import gui.controller.actions.BuyStockAction;
import gui.controller.actions.SellStockAction;
import gui.model.model;

import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

public class buySellFrame extends JFrame{

	private	JTable stockTable;
	private JTable queueTable;
	
	private JPanel topPanel;
	private JPanel btnPanel;
	
	private JButton btnBuy;
	private JButton btnSell;
	
	private Action buyAction;
	private Action sellAction;
	
	private JLabel queue;
	
	String columnNames[] = {"Stocks", "Prices"};

	String dataValues[][] =
	{
		{"OSX", "250"},	//default cell values
		{"Ria", "340"},
		{"NIBC", "290"},
		{"Vienta", "170"},
		{"BHT", "260"},
		{"Cones", "470"},
	};
	
	String columnNamesQueue[] = {"Stock Code", "Remaining Quantity", "Desired Price", "Current Price"};

	String dataValuesQueue[][] =
	{
		{" ", " ", " ", " "},
		{" ", " ", " ", " "}, //empty queue table
		{" ", " ", " ", " "},
		{" ", " ", " ", " "},
		{" ", " ", " ", " "},
		{" ", " ", " ", " "},
	};

	
	public buySellFrame(model m)
	{
		buyAction = new BuyStockAction(m);
		sellAction = new SellStockAction(m); //create actions for buttons
		
		topPanel = new JPanel();
    	topPanel.setPreferredSize(new Dimension(400,400));
    	topPanel.setLayout(new FlowLayout());
    	this.getContentPane().add(topPanel);
    	
		stockTable = new JTable(dataValues, columnNames) 
		{
			public boolean isCellEditable(int rowIndex, int vColIndex) //override function to disable cell editing
			{
				return false;
			}
		};
		
		JScrollPane stockPane = new JScrollPane(stockTable);
		stockPane.setPreferredSize(new Dimension(400,200));
		
		for(int i = 0; i < stockTable.getColumnCount(); i++) //set column width
		{
			stockTable.getColumnModel().getColumn(i).setPreferredWidth(180);
		}
		
		topPanel.add(stockPane);
		
		btnBuy = new JButton(buyAction);
		btnSell = new JButton(sellAction);
		
		btnBuy.setText("Buy Stocks");
		btnSell.setText("Sell Stocks");
		
		btnPanel = new JPanel();
		btnPanel.setLayout(new FlowLayout());
		btnPanel.add(btnBuy);
		btnPanel.add(btnSell); //add everything to the panel
		topPanel.add(btnPanel);
		
		queue = new JLabel("Items in queue:");
		queue.setPreferredSize(new Dimension(400,15));
		topPanel.add(queue);
		
		queueTable = new JTable(dataValuesQueue, columnNamesQueue) 
		{
			public boolean isCellEditable(int rowIndex, int vColIndex) //override function to disable cell editing
			{
				return false;
			}
		};
		
		for(int i = 0; i < queueTable.getColumnCount(); i++) //set column width
		{
			queueTable.getColumnModel().getColumn(i).setPreferredWidth(90);
		}
		
		JScrollPane queuePane = new JScrollPane(queueTable);
		queuePane.setPreferredSize(new Dimension(400,200));
		
		topPanel.add(queuePane);

		
		this.setTitle("Buy/Sell Stocks");
		this.setResizable(false);
		this.setLocationRelativeTo(null); //set frame to middle of screen
    	this.setSize(500,500);
	}
	
	public JTable getStockTable()
	{
		return stockTable;
	}
	
	public JTable getQueueTable()
	{
		return queueTable;
	}
}
